package cn.com.edu.nju.ecrec.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;

import cn.com.edu.nju.ecrec.dao.EventDao;
import cn.com.edu.nju.ecrec.model.Event;
import cn.com.edu.nju.ecrec.util.JPAFactory;

public class EventDaoImpl implements EventDao {

	private EntityManager em;

	public EventDaoImpl() {
		em = JPAFactory.getManager();
	}

	public void finalize() {
		em.close();
	}

	@Override
	public void commit() {
		em.getTransaction().commit();
	}

	@Override
	public void begin() {
		if (em == null)
			em = JPAFactory.getManager();
		if (!em.isOpen())
			em = JPAFactory.getManager();
		if (!em.getTransaction().isActive())
			em.getTransaction().begin();
	}

	@Override
	public List<String> getUserIDsByUriName(String uri_name) {
		this.begin();
		String sql = "SELECT distinct e.userID FROM Event e where e.page like '%"+uri_name+"%'";
		List<String> list = em.createQuery(sql).getResultList();
		return list;
	}

}
